package query.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import query.bean.User;
import query.db.UserRepository;

/**
 * 用户数据库操作接口实现类
 * 
 * @author lzy
 * @version 1.0.4
 *
 */
@Repository
public class JdbcUserRepository implements UserRepository {

	private JdbcTemplate jdbc;
	private final static String SEARCH_USER_BY_INFO = "SELECT* FROM User WHERE userName = ? AND password = ?";
	private final static String SEARCH_USERS = "SELECT* FROM User ORDER BY userName LIMIT ";
	private final static String REGISTER = "INSERT INTO User (userName,password,email,phone,createTime,manager)VALUES(?,?,?,?,?,?)";
	private final static String UPDATE_INFO = "UPDATE User SET email = ?,phone = ? WHERE userName = ?";
	private final static String UPDATE_PASSWORD = "UPDATE User SET password = ? WHERE userName = ?";
	private final static String UPDATE_MANANGER = "UPDATE User SET manager = ? WHERE userName = ?";
	private final static String DELETE_USER = "DELETE FROM User WHERE userName = ?";

	/**
	 * 构造方法注入
	 * 
	 * @param jdbc
	 */
	@Autowired
	public JdbcUserRepository(JdbcTemplate jdbc) {
		this.jdbc = jdbc;
	}

	/**
	 * 查找一个用户
	 * 
	 * @param userName 用户名
	 * @param password 密码
	 * @return 查找结果
	 */
	@Override
	public User search(String userName, String password) {
		User user = null;
		try {
			user = jdbc.queryForObject(SEARCH_USER_BY_INFO, new UserRowMapper(), userName, password);
		} catch (Exception e) {
			return null;
		}
		return user;
	}

	/**
	 * 查找一定范围内的用户
	 * 
	 * @param start 起始下标(不包括在内)
	 * @param num   要查找的用户数目
	 * @return 用户列表
	 */
	@Override
	public List<User> search(int start, int num) {
		List<User> users;
		try {
			users = jdbc.query(SEARCH_USERS + start + "," + num, new UserRowMapper());
		} catch (Exception e) {
			return null;
		}
		return users;
	}

	/**
	 * 注册一个用户
	 * 
	 * @param userName 用户名
	 * @param password 密码
	 * @param email    电子邮箱
	 * @return 操作结果
	 */
	@Override
	public boolean register(String userName, String password, String email) {
		Date date = new Date();
		try {
			jdbc.update(REGISTER, userName, password, email, null, date, false);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 更新用户信息
	 * 
	 * @param userName 用户名
	 * @param email    电子邮箱
	 * @param phone    电话号码
	 * @return 操作结果
	 */
	@Override
	public boolean update(String userName, String email, String phone) {
		jdbc.update(UPDATE_INFO, email, phone, userName);
		return true;
	}

	/**
	 * 修改密码
	 * 
	 * @param userName 用户名
	 * @param password 密码
	 * @return 操作结果
	 */
	@Override
	public boolean update(String userName, String password) {
		jdbc.update(UPDATE_PASSWORD, password, userName);
		return true;
	}

	/**
	 * 修改用户权限
	 * 
	 * @param userName 用户名
	 * @param manager  管理员权限
	 * @return 操作结果
	 */
	@Override
	public boolean update(String userName, boolean manager) {
		jdbc.update(UPDATE_MANANGER, manager, userName);
		return true;
	}

	/**
	 * 删除用户
	 * 
	 * @param userName 用户名
	 * @return 操作结果
	 */
	@Override
	public boolean delete(String userName) {
		jdbc.update(DELETE_USER, userName);
		return true;
	}

	/**
	 * 用于查询用户
	 * 
	 * @author lzy
	 * @version 1.0.4
	 *
	 */
	private static final class UserRowMapper implements RowMapper<User> {
		public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			User user = new User();
			user.setUserName(rs.getString("userName"));
			user.setPassword(rs.getString("password"));
			user.setEmail(rs.getString("email"));
			user.setPhone(rs.getString("phone"));
			user.setCreateTime(rs.getDate("createTime"));
			user.setManager(rs.getBoolean("manager"));
			return user;
		}

	}
}
